/**
 * Created by ROGSTAR on 2016/11/7.
 */
require.config(window.jshead);
require(['jquery', 'pages', "layer",'show','navigator','radialIndicator'], function ($, pg,layer,sw) {
    //$(".addBtn").bind("click",function(){
    //    layer.alert('即将上线，敬请期待！', {
    //        skin: 'layui-layer-molv' //样式类名
    //        ,closeBtn: 0
    //    });
    //});
    $(".addBtn").bind("click",function(){
        $("#provinces").val('请选择');
        $("#city").val('请选择');
        $("#loan_money").val('');
        $("#loan_img").val('');
        getBank();
        $(".loan_alert").show();
        var loan_text= '申请'+$(this).siblings("h2").text()
        $(".loan_alert h2").html(loan_text);
        id=$(this).attr('id');

    });
    $("#loan_alert_close").bind("click",function(){
        $(".loan_alert").hide();
        $("#loan_money").val('');
        $("#loan_img").val('');
    });
    var loan_img=$("#loan_img").val();
    $("#loan_alert_submit").bind("click",function(){
        var loan_name=$("#loan_name").val();
        var loan_phone=$("#loan_phone").val();
        var loan_money=$("#loan_money").val();
        var loan_img=$("#loan_img").val();
        var verification= $('#verification').val();
        if($('#provinces option:selected').val()=="请选择" || $('#city option:selected').val()=='请选择'){
            layer.tips("请选择您所在省市","#city");

            return
        }else if(loan_name==""){
            layer.tips("未获取到借款人姓名，请刷新重试","#loan_name");
            return
        }else if(loan_phone==""){
            layer.tips("未获取到借款人姓名，请刷新重试","#loan_phone");
            return
        }else if(loan_money==""){
            layer.tips("请输入借款金额","#loan_money");
            return
        }else if(loan_money<1000){
            layer.tips("借款金额需高于1000元","#loan_money");
            return
        }else if(loan_img==""){
            layer.tips("请输入图片验证码","#loan_img");
            return
        }else if(verification=''){
            layer.msg('短信验证么不能为空')
        }else{
            var area = $('#provinces option:selected').text()+$('#provinces option:selected').text()
            var verification= $('#verification').val();
            $.ajax({
                type:'post',
                url:'/loan/addLoan',
                data:{
                    mobileCode:verification,
                    area:area,
                    amount:loan_money,
                    loanType:id
                },
                beforeSend: function () {
                    // 禁用按钮防止重复提交
                    $("#loan_alert_submit").attr({disabled:'disabled'});
                },
                dataType:'json',
                success:function(data){
                    if(data.result=='fail'){
                        layer.msg(data.messageText)
                        $("#loan_alert_submit").removeAttr('disabled');
                    }else{
                        layer.msg('成功');
                        $("#loan_alert_submit").attr({disabled:'disabled'});
                        setTimeout(function () {
                            window.location.href="/user/assets";
                        },1000)
                    }
                }
            })
        }
    });



//短信验证码
$('#clickMes').bind('click',function(){
    var loan_name=$("#loan_name").val();
    var loan_phone=$("#loan_phone").val();
    var loan_money=$("#loan_money").val();
    var loan_img=$("#loan_img").val();
    var loan_img=$("#loan_img").val();
    if(loan_img==""){
        layer.msg('图形验证码不能为空')
        return;
    }else if($('#provinces option:selected').val()=="请选择" || $('#city option:selected').val()=='请选择'){
        layer.tips("请选择您所在省市","#city");

        return
    }else if(loan_name==""){
        layer.tips("未获取到借款人姓名，请刷新重试","#loan_name");
        return
    }else if(loan_phone==""){
        layer.tips("未获取到借款人姓名，请刷新重试","#loan_phone");
        return
    }else if(loan_money==""){
        layer.tips("请输入借款金额","#loan_money");
        return
    }else if(loan_img==""){
        layer.tips("请输入图片验证码","#loan_img");
        return
    }
    var that=$(this);
    time(that);
    //短信接口
    $.ajax({
        type: "POST",
        url: "/loanSms",
        data: {
            pictureCode:loan_img
        },
        dataType: "json",
        success: function (data) {
            if(data.result=='success'){
                layer.msg("短信已发送！");

            }else{
                wait=0
                layer.msg(data.messageText);
            }
        }
    });
    //短信接口end
});

var wait=60;
function time(o) {
    if (wait == 0) {
        o.removeAttr("disabled");
        o.val('获取验证码');
        o.css("color", '#ffffff');
        o.css("background","#2c97f4");
        wait = 60;
        $('.validImg').trigger('click');
    } else {
        o.attr("disabled", true);
        o.css("color", '#fff');
        o.css("background", '#ddd');
        o.val("重新发送(" + wait + "s)");
        wait--;
        setTimeout(function() {
                time(o)
            },
            1000)
    }
}

























    //获取常见问题的信息
    $.ajax({
        type:'post',
        url:'/problem/problemList',
        dataType:'json',
        data:{type:12},
        success:function(data){
            var loan_problem_list = ''
            var j =data.list.length>=4?4:data.list.length;

            for(var i = 0;i<j;i++){
                var answer = data.list[i].answer.replace(/(\n)/g, "").replace(/(\t)/g, "").replace(/(\r)/g, "").replace(/<\/?[^>]*>/g, "").replace(/\s*/g, "");
                var problem = data.list[i].problem.replace(/(\n)/g, "").replace(/(\t)/g, "").replace(/(\r)/g, "").replace(/<\/?[^>]*>/g, "").replace(/\s*/g, "");
                if(problem.length>40){
                    problem = problem.substr(0,40)+'...'
                }
                if(answer.length>13){
                    answer = answer.substr(0,12)+'...?'
                }
                if(i%2 ==0){
                }
                 loan_problem_list =loan_problem_list+
                     '<div class="loan_problem_list">' +
                     '<h1><a style="color:#424242;" href="/problem?0?5?'+data.list[i].type+'?'+data.list[i].id+'">'+answer+'</a></h1><p>'+problem+'</p></div>'
            }
            $('.loan_problem').html(loan_problem_list);
        }

    })

    //借款金额校验
    $('#loan_money').keyup(function(){
        var aa =  $('#loan_money').val()
        var reg =/^[1-9]*[1-9][0-9]*$/
        if(!reg.test(aa)){
            layer.tips('请输入合法的借款金额','#loan_money');
            $('#loan_money').val('')
        }else if(aa>1000000){
            layer.tips('最多可借100万','#loan_money');
            $('#loan_money').val(1000000)
        }
    })

    function getProvinces(){
        $.ajax({
            type: "POST",
            url: "/loan/areaList",
            data: {pid:0},
            dataType: "json",
            success: function (data) {
                if(data.result=="fail"){
                    layer.tip("数据请求失败");
                }else{
                    var list='<option value="请选择">请选择</option>';
                    for(var j=0;j<data.length;j++){
                        list+='<option value="'+data[j].id+'">'+data[j].name+'</option>'
                    }
                    $("#provinces").html(list);
                    $("#provinces").bind("change",function(){
                        var pid=$('#provinces option:selected') .val();
                        $.ajax({
                            type: "POST",
                            url: "/loan/areaList",
                            data: {pid:pid},
                            dataType: "json",
                            success: function (data) {
                                if(data.result=="fail"){
                                    layer.tip("数据请求失败");
                                }else{
                                    var list='<option value="请选择">请选择</option>';
                                    for(var j=0;j<data.length;j++){
                                        list+='<option value="'+data[j].id+'">'+data[j].name+'</option>'
                                    }
                                    $("#city").html(list);
                                }

                            }
                        });
                    });

                }

            }
        });
    }
    getProvinces();
    //图片验证
    $('.validImg').click(function() {
        $(".validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
    });
    //获取绑卡情况
    function getBank(){
        $.ajax({
            type:'post',
            url: "/login/isLogin",
            dataType:'json',
            success:function(data){
                if(data.result=='fail'){
                    layer.msg('请先登录');
                    function jump() {
                        window.location.href = '/login'
                    }
                    window.setTimeout(jump, 1500);
                }else{
                    if (data.security.identifyCard == null) {
                        layer.msg('未绑定实名认证，即将跳转至实名认证页面');
                        function jump() {
                            window.location.href = '/user/security/realname'
                        }
                        window.setTimeout(jump, 1500);
                    }else{
                        $.ajax({
                            type: "POST",
                            url: "/account/bank/getBankByUserId",
                            dataType: "json",
                            success: function (data1) {
                                if(data1.result=='fail'){
                                    layer.msg('未绑定银行卡，即将跳转至银行卡页面');
                                    function jump(){
                                        window.location.href ='/user/security/realname'
                                    }
                                    window.setTimeout(jump,1500);
                                }else{
                                    $("#loan_name").val(data.security.realname);
                                    $("#loan_phone").val(data.user.mobile);
                                    var gg = data.security.identifyCard.substr(14,3)%2?'男':'女'
                                    if(gg=='男'){
                                        $('.boy').addClass('cur');
                                        $('.girl').removeClass('cur');
                                    }else if(gg == '女'){
                                        $('.boy').removeClass('cur');
                                        $('.girl').addClass('cur');
                                    }
                                }
                            }
                        });
                    }
                }
            }
        })
    };
});